I need to call cURL multiple times to get different JSON responses so I followed the code in the answer of this question:
Multiple curl json and json print.
Now I realize that the variable that stores the return data is only holding the information from the last URL in the array (it's been overwritten). Here is my code:
$urls = Array(
'https://example.com/projects/277199/roles.json',
'https://example.com/projects/292291/roles.json'
);
foreach ($urls as $key=>$url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "XXX:YYY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ch_response = curl_exec($ch);
curl_close($ch);
$rolesData = json_decode($ch_response,true);
}
print_r($rolesData); //It's only printing the data from the last element in the urls array
How do I correctly store the data?
just add [] or [$key] to $rolesData
foreach ($urls as $key=>$url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "XXX:YYY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ch_response = curl_exec($ch);
curl_close($ch);
$rolesData[] = json_decode($ch_response,true);
}
print_r($rolesData);
Related
I want to send a data using curl and return some data
I'am using the following code
$data = array('type' => 'active');
$result = '';
$url = 'https://example.com/'.$function_name;
$send_data = json_encode($data, TRUE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $send_data);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);exit;
On the receiving end returning the POST data
print_r($_POST);exit;
But I'am getting data in different structure as below
(
[{"type":"active"}] =>
)
I think due to this $_POST['type'] is not getting in the receiving side
I am trying to fetch data from webservice(nodejs) using curl in php but i am getting result "lang is required"
i tried with following code but not working for me where i am wrong ?
Here is my code
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="http://xxxxxx:8000/api/employer/login";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
Usually to send post variables i use : the http_build_query function.
$url = "http://xxxxxx:8000/api/employer/login";
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
It works better.
first check the request whether send post field or not.
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="https://httpbin.org/post";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
the site: https://httpbin.org/ will response the post field if the post data exists.
I think the problem may occurs the nodejs side. Does it check the post field rightly ?
from my experience your code is right.
Hi i need to make a curl Request via PHP passing a XML by POST but I have no idea how to do that anyone have any idea ?
what i have right now
$xml = '<project>
...
</project>';
$url = 'http://login:token#localhost:8080/createItem?name=newjobname';
$fields = array(
'name' => urlencode('newjobname'),
);
$fields_string = 'name=newjobname';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$result = curl_exec($ch);
curl_close($ch);
Remove the login from the $url and add this to your curl:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "login:token");
Below I have a piece of code to return some information through an API.
$page = 'http://api.duedil.com/sandbox/v2/company/03977902.json? fields=get_all&api_key=***********';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
This prints out the following results http://www.visitrack.co.uk/testdata.php
what i am trying to do is split the results in to individual variables.
Thanks
Something like this should do the trick:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // forces curl_exec() to return page as a string
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, TRUE); // parse json string to array
print_r($data);
I'm having trouble retrieving data from the google api. When I run the code, it only returns a blank page and not a printout of the xml array. Here is the code:
$url="http://www.google.com/ig/api";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "?weather=london,england");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r($data);
I think that the problem is that you use POST method, and not the GET
Try like this
$url="http://www.google.com/ig/api?weather=london,england";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
Hope it helps :)
EDIT: And yes, you have to do some extra parsing to get the data from the XML string