0

I need to send data to another domain. Now I use jquery and ajax

var post_data = {
   foo: "foo",
   bar: "bar"
};

jQuery.ajax({
   url:"https://notmydomain.com/polling/?d=jsonp",
   crossDomain:true,
   dataType:"jsonp",
   cache:false,
   data: post_data,
   type: "post"
});

It works fine, but I need to send it using php. Try to use this solution:

$post_data = http_build_query(
  array(
    'foo' => 'foo',
    'bar' => 'bar'
  )
);

$opts = array(
  'http' => array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $post_data
  )
);
$context  = stream_context_create($opts);
$result = file_get_contents('https://notmydomain.com/polling/?d=jsonp', false, $context);

But it's not work. I'm think cause of specific options in ajax like:

crossDomain:true,
dataType:"jsonp",
cache:false

Don't know how to solve it, need help!

phpCoder
  • 23
  • 4
  • 1
    Why wouldn't you use cURL? – Jay Blanchard Dec 09 '15 at 18:45
  • 1
    ajax is just http. what you're doing is a plain old http request. ajax is specifically for browser->server requests. server->server isn't ajax. – Marc B Dec 09 '15 at 18:54
  • I just tried your code, changing the url to point to a local php file, and it looks like it worked for me. I suspect it has something to do with your server not being able to make https request because the ssl wrapper is not enabled. Can you do it with http instead and print out the content of $result. – Cave Johnson Dec 09 '15 at 19:36
  • [This might help you if it is indeed the ssl extension](https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-work-with-https). There is a answer that has 81 upvotes that shows how to enable the extension. – Cave Johnson Dec 09 '15 at 19:39

0 Answers0