I am trying to set 2 variables to the curl headers and can't seem to see what's wrong. I am not receiving any errors in the php logs, but when I print out the curl info I can see that the headers are not being set. Any point in the right direction would be helpful. Thanks
The Example I'm using
PHP cURL custom headers
class GetAuctions
{
private $APIKeyID = "theIDhere";
private $APIKeyPass = "thePasswordHere";
private $BaseURL = "https://someurlHere";
public function __construct()
{
//get list of upcoming auctions
$get_data = $this->callAPI('GET', $this->BaseURL, false);
//turn the response into a json
$response = json_decode($get_data, true);
//display the response for testing
echo print_r($response);
$errors = $response['response']['errors'];
$data = $response['response']['data'][0];
echo print_r($data);
}
function callAPI($method, $url, $data){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
$headers =array();
$headers['apiKeyID'] = $this->APIKeyID;
$headers['apiKeyPass'] = $this->APIKeyPass;
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
echo "<br/>";
echo print_r(curl_getinfo($curl));
echo "<br/>";
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
}
}
I've also tried this:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"apiKeyID: $this->APIKeyID",
"apiKeyPass: $this->APIKeyPass"
));
My response looks like this:
Array ( [url] => https://MyURLHere [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] => [primary_ip] => [certinfo] => Array ( ) [primary_port] => 0 [local_ip] => [local_port] => 0 ) 1
Connection Failure
It looks like your headers are incorrectly formatted:
"apiKeyID : $this->APIKeyID",
You should remove space before your colon:
"apiKeyID: ${this->APIKeyID}",
BTW: for debugging purpose you may also use CURLOPT_VERBOSE to see what's is being send by cURL. If you cannot peek stderr at runtime, redirect it to the file:
curl_setopt($c, CURLOPT_STDERR, fopen('curl-log.txt', 'w+'));
Related
I apologize in advance for my English.
I need from a url http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525 get the final redirected to url redirection (.flv) url.
I tried this, but somehow it doesn't get the info I need:
<?php
function getMainUrl($url) {
$headers = get_headers($url, 1);
return $headers['Location'];
}
echo getMainUrl("http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525");
?>
When use:
http://getlinkinfo.com/info?link=http%3A%2F%2Fwww.streamuj.tv%2Fvideo%2F2f15014c90a9f62af511%3Fstreamuj%3Dhd%26authorize%3D7736cdf0f3719ed75b26132aee184525+&x=45&y=6
it redirects to:
http://s14.streamuj.tv:8080/vid/8f18cade6df7fc2d54a3522e7515771e/58a1ecfa/2f15014c90a9f62af511_hd.flv?start=0
and this is the one I need from php
Any help would be appreciated. Thanks
Using cURL, we tell it only to fetch the header and to follow redirects.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525');
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
print_r($info);
Outputs:
Array
(
[url] => http://s14.streamuj.tv:8080/vid/c7194f1279591f88c162382a0a5a49d1/58a1f0a5/2f15014c90a9f62af511_hd.flv?start=0
[content_type] => video/x-flv
...
)
This Work:
<?php
session_start();
include "simple_html_dom.php";
$proxy = array("88.159.43.160:80");
$proxyNum = 0;
$proxy = explode(':', $proxy[$proxyNum]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.streamuj.tv/video/2f15014c90a9f62af511?streamuj=hd&authorize=7736cdf0f3719ed75b26132aee184525');
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_PROXY, $proxy[0]);
curl_setopt($curl, CURLOPT_PROXYPORT, $proxy[1]);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
print_r($info);
?>
Outputs:
Array ( [url] => http://s14.streamuj.tv:8080/vid/0b9ac442e33b2f0fe72dd45295b6e7bd/58a1f3b0/2f15014c90a9f62af511_hd.flv?start=0 [content_type] => video/x-flv [http_code] => 200 [header_size] => 738 [request_size] => 326 [filetime] => 1481886073 [ssl_verify_result] => 0 [redirect_count] => 1 [total_time] => 3.485 [namelookup_time] => 0 [connect_time] => 3.063 [pretransfer_time] => 3.063 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 674855667 [upload_content_length] => -1 [starttransfer_time] => 3.188 [redirect_time] => 0.297 [redirect_url] => [primary_ip] => 88.159.43.160 [certinfo] => Array ( ) [primary_port] => 80 [local_ip] => 192.168.0.8 [local_port] => 57532 )
How to can please echo only this: http://s14.streamuj.tv:8080/vid/0b9ac442e33b2f0fe72dd45295b6e7bd/58a1f3b0/2f15014c90a9f62af511_hd.flv?start=0
?
I need to send a cURL request containing a file to upload as well as a JSON string. I can get a properly formatted request when sending the JSON, but the file is throwing it off.
$postData = array(
'JSON'=> json_encode($jsonParams),
$reference => '#'.$tmp_file_path
);
//Borrowed function from: http://scraperblog.blogspot.com/2013/07/php-curl-multipart-form-posting.html
function multipart_build($fields, $boundary){
$retval = '';
foreach($fields as $key => $value){
$retval .= "--$boundary\nContent-Disposition: form-data; name=\"$key\"\n\n$value\n";
}
$retval .= "--$boundary--";
return $retval;
}
$boundary = "--requestboundary-xxx";
$request = $this->multipart_build($postData,$boundary);
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
The $reference needs to be tied to a value in the $jsonParams so the receiving server can ensure uploaded files are attached to the corresponding data string packages.
////////////////////////////////////////////////////////////
Update after Marc B's comment
////////////////////////////////////////////////////////////
So my first approach wasn't using my own multipart. The reason I went with multipart was at least I was getting a response from the server. Here is the original code.
$curl_file_object = curl_file_create($attachmentFile['tmp_name'], $attachmentFile['type'], $reference);
$postData= array(
'JSON'=> json_encode($jsonParams),
$reference => $curl_file_object,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data;"));
curl_setopt($curl, CURLOPT_POST, true); // enable posting
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); // post data
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
debug($info);
And with this I get:
[url] => --------
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.031
[namelookup_time] => 0
[connect_time] => 0.031
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => --------
[certinfo] => Array
(
)
[primary_port] => --------
[local_ip] => --------
[local_port] => --------
With the multipart I was at least getting a 400.
Don't built your own multipart. Curl is perfectly capable of doing that for you:
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
it'll take an array and do whatever's needed automatically.
And if you're on PHP 5.5+, you can use curlfile for the file part as well, without the # hack.
Using CURLFile instead of curl_file_create() fixed the problem. Even though they are supposed to be the same...
I am trying to bring up a website/API through cURL and PHP. Whatever URL I try I am getting an HTTP Code of 0. I have tried several different URLs. No matter what I try I am getting the following curl_getinfo back (see below). I have verified that cURL is enabled in PHP.ini file.
Code:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.yahoo.com");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$report=curl_getinfo($ch);
print_r($report);
// grab URL and pass it to the browser
curl_exec($ch);
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
print curl_error($ch);
// close cURL resource, and free up system resources
curl_close($ch);
cURL_getinfo:
Array
(
[url] => http://www.yahoo.com
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => 0
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
[primary_ip] =>
[primary_port] => 0
[local_ip] =>
[local_port] => 0
[redirect_url] =>
)
You need to call curl_exec($ch); before curl_getinfo($ch); cause this is the actual connection to the server:
also there is no need in the flag CURLOPT_POST since it's a get call:
// create a new cURL resource
$ch = curl_init();
//for post calls:
//$post = 'a=b&d=c';
//$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=utf-8';
//$headers[] = 'Content-Length: ' . strlen($post);
//for get calls:
$headers = array();
$headers[] = 'Content-type: charset=utf-8';
$headers[] = 'Connection: Keep-Alive';
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.yahoo.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_exec($ch);
$report=curl_getinfo($ch);
print_r($report);
// grab URL and pass it to the browser
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
print curl_error($ch);
// close cURL resource, and free up system resources
curl_close($ch);
I am trying to use The OneAPI SMS interface to send SMS messages from my application using cURL.
It returns 500 error and here is the code I am using:
<?php
$url = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A%2B1234/requests";
$username = "secret";
$password = "secret";
$request = array(
'address' => 'tel%3A%2B1222333444',
'message' => 'hello world',
'senderAddress' => 'tel%3A%2B1234',
'senderName' => 'joe doe'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($info);
?>
Any help woudl be any help would be greatly appreciated. Thank you!
P.S.
Array ( [url] => https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A%2B1416XXXYYYY/requests
[content_type] => application/json [http_code] => 500 [header_size] => 494 [request_size] => 349
[filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.333515 [namelookup_time] => 0.000335
[connect_time] => 0.054989 [pretransfer_time] => 0.229044 [size_upload] => 97 [size_download] => 172 [speed_download] => 515
[speed_upload] => 290 [download_content_length] => -1 [upload_content_length] => 97 [starttransfer_time] => 0.333474 [redirect_time] => 0 [certinfo] => Array ( ) )
Solved
For the ancestors: for some reasons cURL is not working, so file_get_contents() is the way to go.
If you are posting parameter values in JSON you shouldn't URL encode the values. This is necessary if using 'form post's but not for JSON.
Try making the same request using command line curl - you should be able to get the details of the server error accompanying the 500 return code.
Apparently, I solved this puzzle with cURL. The problem was in its syntax. Heres the working PHP solution for their APIs.
Class SendSMS {
public $phone_number;
public function __construct($phone_number) {
$this->phone_number = $phone_number;
}
public function oneAPI() {
$url = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A7511/requests"; // END POINT
$username = 'secret'; // APP's login
$password = 'secret'; // and psswd
$request = array(
'address' => $this->phone_number,
'message' => 'hello world',
'senderAddress' => 'tel:7511'
);
$request = http_build_query($request);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // hide output
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
}
}
$hello = new SendSMS('+1XXXXXXXXX'); // WHITE LISTED PHONE
$hello->oneAPI();
With every Twitter request I make, the returned HTTP headers should include X-RateLimit-Limit.
However, I seem unable to retrieve these using PHP. Can someone tell me what bone-headed mistake I've made?
I've set my curl up in the normal way and am able to successfully GET and POST requests.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$response = curl_exec($ch);
$response_info=curl_getinfo($ch);
$erno = curl_errno($ch);
$er = curl_error($ch);
curl_close($ch);
I'm able to get some response information, like http_code
$response_info['http_code']
But this line just returns null
//Doesn't bloody work. No idea why!
$rate_limit = $response_info['X-RateLimit-Limit'];
I'm running PHP Version 5.3.10.
EDIT
This is the result of print_r($response_info);
Array
(
[url] => https://api.twitter.com/1/statuses/home_timeline.json...
[content_type] => application/json;charset=utf-8
[http_code] => 200
[header_size] => 695
[request_size] => 410
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.239977
[namelookup_time] => 0.007361
[connect_time] => 0.155783
[pretransfer_time] => 0.465397
[size_upload] => 0
[size_download] => 99425
[speed_download] => 80182
[speed_upload] => 0
[download_content_length] => 99425
[upload_content_length] => 0
[starttransfer_time] => 0.794829
[redirect_time] => 0
[certinfo] => Array()
[redirect_url] =>
[request_header] => GET /1/statuses/home_timeline.json... HTTP/1.1
Host: api.twitter.com
Accept: */*
)
curl_getinfo does not return the response headers, only other meta info about the request. To retrieve headers, set CURLOPT_HEADER to true. That will include the headers in the output. To separate them from the response body do:
list($headers, $body) = explode("\n\n", $response, 2);
To parse the headers, explode again:
$headers = explode("\n", $headers);
foreach ($headers as $header) {
list($key, $value) = explode(':', $header, 2);
$headers[trim($key)] = trim($value);
}
echo $headers['X-RateLimit-Limit'];